<extend name="Layout:layout_edit" />
<block name="body_content_block">
    <form action="{:AddonU('Queue/item_save')}" method="post" data-success-fn="show_reload_tip()">
        <table>
            <if condition="$isSuperAdmin">
                <th>租户ID：</th><td><select name="siteID" title="请选择租户" required data-url="{:AddonU('Admin/get_admin_json')}" data-value="{$data.siteID}"></select>
                <else/>
                <tr style="display: none;"><input type="hidden" name="siteID" value="{$loginData.siteID}"/></td></tr>
            </if>
            <tr>
            <tr>
                <th>队列号码：</th><td>
                    <input type="text" name="extension" placeholder="队列号码" value="{$data.extension}" required data-url="{:AddonU('Status/cti_check_extension')}" size="8"/><span style="color:#F00;" id="name-tip"></span>
                </td></tr>
            <tr>
                <th>超时溢出标志：</th><td>
                    <select name="overflowStrategy" title="请选择溢出标志">
                        <option value='0' selected='selected'>不溢出</option>
                        <option value='queue'>溢出到队列</option>
                        <option value='extension'>溢出到分机</option>
                    </select>
                </td></tr>
            <tr>
                <th>溢出目标：</th><td>
                    <select name="overflowTarget" title="请选择..." data-url="{:AddonU('Queue/get_queue_json')}" data-url2="{:AddonU('Extension/get_extension_json')}" data-value="{$data.overflowTarget}"></select>
                </td></tr>
            <tr>
                <th>欢迎语音：</th><td>
                    <select name="music" title="请选择欢迎语音" data-url="{:AddonU('Music/get_subdir_json')}" data-value="{$data.music}"></select>
                </td></tr>
            <tr>
                <th>队列全满等待音：</th><td>
                    <select name="over_music" title="请选择队列全满等待音" data-url="{:AddonU('Music/get_subdir_json')}" data-value="{$data.over_music}"></select>
                </td></tr>
            <tr>
                <th>队列等待超时：</th><td>
                    <select name='maxwait' title="请选择队列等待超时时间(秒)!">
                        <option value='0'>不限</option>
                        <option value='10'>10</option>
                        <option value='20'>20</option>
                        <option value='30'>30</option>
                        <option value='40'>40</option>
                        <option value='50'>50</option>
                        <option value='60'>60</option>
                        <option value='90'>90</option>
                        <option value='120' selected='selected'>120</option>
                        <option value='150'>150</option>
                        <option value='180'>180</option>
                    </select>(秒)
                </td></tr>
            <tr>
                <th>最大排队数量：</th><td>
                    <select name='maxlen' title="请选择最大排队数量!"></select>
                    <script type='text/javascript'>
                        var opts = '';
                        for (var i = 0; i < 51; i++) {
                            opts += '<option value="' + i + '"' + (i == 0 ? ' selected="selected"' : '') + '>' + i + '</option>';
                        }
                        $('select[name="maxlen"]').html(opts);
                    </script>
                </td></tr>
            <tr>
                <th>振铃规则：</th><td>
                    <select name="strategy" title="请选择振铃规则">
                    <option value="random" selected="selected">随机</option>
                    <option value="ringall">呼叫所有</option>
                    <option value="rrmemory">记忆呼叫</option>
                    <!--<option value="roundrobin">轮询</option>
                    <option value="leastrecent">呼叫接听最少</option>
                    <option value="fewestcalls">呼叫完成最少</option>
                    <option value="rrordered">rrordered</option>
                    <option value="linear">linear</option>
                    <option value="wrandom">wrandom</option>-->
                    </select>
                </td></tr>
            <tr>
                <th>重试间隔：</th><td>
                    <select name='retry' title="请选择重试间隔(秒)!"></select>(秒)
                    <script type='text/javascript'>
                        opts = '<option value="none">不重试</option>';
                        for (var i = 0; i < 21; i++) {
                            opts += '<option value="' + i + '"' + (i == 0 ? ' selected="selected"' : '') + '>' + i + '</option>';
                        }
                        $('select[name="retry"]').html(opts);
                    </script>
                </td></tr>
            <tr>
                <th>座席振铃超时：</th><td>
                    <select name='timeout' title="请选择座席振铃超时时间(秒)!"></select>(秒)
                    <script type='text/javascript'>
                        opts = '<option value="0">不限</option>';
                        for (var i = 1; i < 61; i++) {
                            opts += '<option value="' + i + '"' + (i == 60 ? ' selected="selected"' : '') + '>' + i + '</option>';
                        }
                        $('select[name="timeout"]').html(opts);
                    </script>
                </td></tr>
            <tr style="display: none;">
                <th>跳过示忙座席：</th><td>
                    <input type="radio" name="cwignore" value="1" checked="checked" required/>是
                    <input type="radio" name="cwignore" value="0" required/>否
                </td></tr>
            <tr style="display: none;">
                <th>录音格式：</th><td>
                    <select name="monitor-format">
                        <option value=''>无</option>
                        <option value="wav49" selected="selected">wav49</option>
                        <option value="wav">wav</option>
                        <option value="gsm">gsm</option>
                    </select>
                </td></tr>
            <tr style="display: none;">
                <th>录音模式：</th><td>
                    <select name="monitor_type" tabindex="21">
                        <option value="">Include Hold Time</option>
                        <option value="b" selected="selected">After Answered</option>
                    </select>
                </td></tr>
            <tr style="display: none;">
                <th>Autofill：</th><td>
                    <input name="autofill" type="checkbox" value="1" checked="checked">
                </td></tr>
            <tr style="display: none;">
                <th>复位队列状态：</th><td>
                    <select name="cron_schedule">
                        <option value="never">永不</option>
                        <option value="hourly">每小时</option>
                        <option value="daily" selected="selected">每日</option>
                        <option value="weekly">每周</option>
                        <option value="monthly">每月</option>
                        <option value="annually">每年</option>
                        <option value="reboot">重启后</option>
                        <option value="custom">自定义</option>
                    </select>
                </td></tr>
            <tr>
                <th>备注：</th><td>
                    <input type="text" name="remark" placeholder="请输入备注" value="{$data.remark}" title="请填写备注!"/>
                </td></tr>
        </table>
        <input type='hidden' name='id' value='{$data.id}'/>
    </form>
</block>
<block name="body_after_block">
    <script>
        if ($('input[name="id"]').val()) {
            $('input[name="extension"]').prop('readonly', true);
        } else {
            $('input[name="extension"]').blur(function () {
                var $this = $(this);
                if (!$this.val()) {
                    return;
                }
                $.get($this.data('url'), {"extension": $this.val()}, function (data) {
                    if (data.status) {
                        $('#name-tip').text('');
                    } else {
                        $('#name-tip').text(data.info);
                        $this.val('');
                    }
                })
            })
        }
        $('select[name="monitor-format"] option[value="{$data[\'monitor-format\']}"]').prop('selected', true);
        $('select[name="monitor_type"] option[value="{$data[\'monitor_type\']}"]').prop('selected', true);
        $('select[name="cron_schedule"] option[value="{$data[\'cron_schedule\']}"]').prop('selected', true);
        $('select[name="maxwait"] option[value="{$data[\'maxwait\']}"]').prop('selected', true);
        $('select[name="maxlen"] option[value="{$data[\'maxlen\']}"]').prop('selected', true);
        $('select[name="retry"] option[value="{$data[\'retry\']}"]').prop('selected', true);
        $('select[name="timeout"] option[value="{$data[\'timeout\']}"]').prop('selected', true);
        $('select[name="strategy"] option[value="{$data[\'strategy\']}"]').prop('selected', true);
        // 超时溢出标志
        $('select[name="overflowStrategy"] option[value="{$data[\'overflowStrategy\']}"]').prop('selected', true);
        set_overflowTarget('{$data["overflowStrategy"]}');
        $('select[name="overflowStrategy"]').change(function () {
            var v = $(this).val();
            set_overflowTarget(v);
        })

        ajax_load_options({selector: 'select[name="siteID"]', def_option: '选择租户', func: afterSiteID});
        afterSiteID();
        function afterSiteID() {
            var val = $('select[name="siteID"]').val();
            if (!val) {
                val = $('input[name="siteID"]').val();
            }
            if (!val) {
                return;
            }
            ajax_load_options({selector: 'select[name="music"]', queryParams: {type: "moh", siteID: val}, prepends: '<option value="default">default</option>'});
            ajax_load_options({selector: 'select[name="over_music"]', queryParams: {type: "moh", siteID: val, field: "pathdir"}, prepends: '<option value="default">default</option>'});
        }
        $('select[name="siteID"]').change(function () {
            afterSiteID();
        })
        function set_overflowTarget(overflowStrategy) {
            var val = $('select[name="siteID"]').val();
            if (!val) {
                val = $('input[name="siteID"]').val();
            }
            if (!val) {
                return;
            }
            switch (overflowStrategy) {
                case '0':
                    $('select[name="overflowTarget"]').empty();
                    break;
                case 'queue':
                    ajax_load_options({selector: 'select[name="overflowTarget"]', queryParams: {siteID: val}, def_option: '请选择队列号'});
                    break;
                case 'extension':
                    ajax_load_options({selector: 'select[name="overflowTarget"]', url: $('select[name="overflowTarget"]').data('url2'), queryParams: {siteID: val}, def_option: '请选择分机号'});
                    break;
            }
        }
    </script>
</block>